﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Expressions
{
    class Excersises
    {
        public static void PrintWordsContainingTest(string text, string substring)
        {
            string pattern = @"\b\w*    #prefix" + Environment.NewLine
                             + Regex.Escape(substring)
                             + @"\w*\b  #suffix" + Environment.NewLine;
            Regex regex = new Regex(pattern, RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase);
            MatchCollection matches = regex.Matches(text);
            foreach (Match match in matches)
            {
                Console.WriteLine(match);
            }
        }

        public static bool isDouble(string number)
        {
            string pattern = @"\A(
                                ((-?) [1-9]+ [0-9]* (\.[0-9]+)? )
                                |
                                (0(\.[0-9]+)?)
                                |
                                (-0\.[0-9]*[1-9]+[0-9]*)
                                )\Z
                                ";

            Regex regex = new Regex(pattern, RegexOptions.IgnorePatternWhitespace);
            return regex.IsMatch(number);
        }

        public static void PrintEmails(string text)
        {
            string emailPattern = @"\b (?<identifier> [a-z]+[\w-]*) @ (?<host> [a-z]+[\w-]*) \. (?<domain> ([a-z]+\w*\.)*\w{1,4}) \b";
            Regex emailRegex = new Regex(emailPattern, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
            MatchCollection emails = emailRegex.Matches(text);
            Console.WriteLine("Found {0} emails", emails.Count);
            foreach (Match match in emails)
            {
                Console.WriteLine(match);
            }
        }

        public static void PrintUrls(string text)
        {
            string urlPattern = @"
                                (?<protocol> [a-z]+)
                                ://
                                (?<server> (\w*\.)+\w{1,3})
                                (?<resource> (/\w+)*\.\w*)*
                                ";
            Regex urlRegex = new Regex(urlPattern, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
            MatchCollection urls = urlRegex.Matches(text);
            Console.WriteLine("Found {0} urls", urls.Count);
            foreach (Match match in urls)
            {
                Console.WriteLine(match);
                Console.WriteLine("Protocol: {0}",match.Groups["protocol"]);
                Console.WriteLine("Server: {0}", match.Groups["server"]);
                Console.WriteLine("Resource: {0}", match.Groups["resource"]);
            }
        }

        static void Main()
        {
            PrintUrls(@"http://www.devbg.org/forum/index.php ");
        }
    }
}